package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Api(value = "菜品管理")
@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    public static final String DISH = "dish_";
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dto) {
        log.info("新增菜品，参数：{}", dto);

        //1.保存菜品信息
        dishService.saveWithFlavor(dto);

        // 清除菜品分类列表
        String key = DISH + dto.getCategoryId();
        redisTemplate.delete(key);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult<DishVO>> page(DishPageQueryDTO dto) {
        log.info("分页查询菜品，参数：{}", dto);

        PageResult<DishVO> dishVOPageResult = dishService.pageQuery(dto);
        return Result.success(dishVOPageResult);
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id获取菜品")
    public Result<List<DishVO>> list(Long categoryId) {
        log.info("根据分类id获取菜品，参数：{}", categoryId);
        List<DishVO> dishes = dishService.getByCategoryId(categoryId);
        return Result.success(dishes);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> get(@PathVariable("id") Long id) {
        log.info("根据id查询菜品，参数：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dto) {
        log.info("修改菜品，参数：{}", dto);
        dishService.updateWithFlavor(dto);

        // 因为可能修改了分类
        // 将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache(DISH + "*");

        return Result.success();
    }

    @PostMapping("status/{status}")
    @ApiOperation("菜品起售、停售")
    public Result enableOrDisable(@PathVariable("status") Integer status, @RequestParam("id") Long id) {
        log.info("{}修改菜品，参数：{}", id, status);
        dishService.enableOrDisable(id, status);

        // 省的再查找具体分类id
        cleanCache(DISH + "*");

        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除菜品{}", ids);
        dishService.delete(ids);

        // 将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache(DISH + "*");
        return Result.success();
    }

    private void cleanCache(String key) {
        // 将所有的菜品缓存数据清理掉，所有以dish_开头的key
        Set dishKeys = redisTemplate.keys(key);
        redisTemplate.delete(dishKeys);
    }
}
